<?php
require_once("dao/tournaments_dao.php");
require_once("dao/brackets_dao.php");

class bracket_controller extends abstract_controller {

	function prepare_model($request_params, Smarty $smarty_obj) {
		

		$smarty_obj->assign("page_title", "Kampoppsett");
			
		
		$name = isset($_GET['name']) ? htmlentities($_GET['name']) : "";
		$user = isset($_SESSION['user']) ? $_SESSION['user'] : "";

		if(tournaments_dao::userRegistered($name,$user) && !(tournaments_dao::hasLost($user,$name)) && brackets_dao::hasOpponent($user,$name)) {
			$smarty_obj->assign("report_loss", true);
		}
		else {
			$smarty_obj->assign("report_loss		", false);
		}
		
		$smarty_obj->assign("name",$name);	
	
		$full = tournaments_dao::addPlayer($name ,$user,true);

	//	if (empty($name) || !is_null($full)){
	//		header("Location: ?page=tournaments");
	//	}

		$bracket = brackets_dao::getBracket($name);
	//	echo "<pre>";
	//	print_r($bracket);
		echo "</pre>";
	
		if (is_null($bracket)){
			brackets_dao::generateBracket(tournaments_dao::readTournament($name));
			$bracket = brackets_dao::getBracket($name);
		}
//		echo "<pre>";
//		print_r($bracket);
		
		$hmm = array();
		$padding = array();
		foreach($bracket->round as $round) {
			$hmm[] = $round;
		}
		/*for ($x=0; $x<count($hmm); $x++){
			$tmp = (100-(100/(pow(2,$x))))/(4);
			echo $tmp."<br/>";
			$padding[] = $tmp;
		}*/
		$start = 20;
		$tmp = array();
		for ($x=0; $x<count($hmm); $x++){
			$tmp[] = (($x+1)*$start)+(pow(2,$x)*36)-36;
			echo $tmp[$x]." ";
			//$start *= 2;
		}
	
		
		$smarty_obj->assign("rounds",$hmm);
		$smarty_obj->assign("padding",$tmp);
		$smarty_obj->assign("tmp_rounds_tingert",floor(90/count($hmm)));

	//	print_r($padding);
	//	echo $padding[2];
		
			
	}
}

?>
